<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是Linux中应用层直接使用I2C驱动的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV10-11-I2C驱动-02-应用层使用I2C驱动">
<meta property="og:url" content="https://sumumm.github.io/post/18b93c9.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是Linux中应用层直接使用I2C驱动的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-11-I2C%E9%A9%B1%E5%8A%A8-02-%E5%BA%94%E7%94%A8%E5%B1%82%E4%BD%BF%E7%94%A8I2C%E9%A9%B1%E5%8A%A8/img/image-20220927162633308.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-11-I2C%E9%A9%B1%E5%8A%A8-02-%E5%BA%94%E7%94%A8%E5%B1%82%E4%BD%BF%E7%94%A8I2C%E9%A9%B1%E5%8A%A8/img/01.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-11-I2C%E9%A9%B1%E5%8A%A8-02-%E5%BA%94%E7%94%A8%E5%B1%82%E4%BD%BF%E7%94%A8I2C%E9%A9%B1%E5%8A%A8/img/02.png">
<meta property="article:published_time" content="2023-07-08T10:58:38.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.005Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV10-驱动开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-11-I2C%E9%A9%B1%E5%8A%A8-02-%E5%BA%94%E7%94%A8%E5%B1%82%E4%BD%BF%E7%94%A8I2C%E9%A9%B1%E5%8A%A8/img/image-20220927162633308.png">


<link rel="canonical" href="https://sumumm.github.io/post/18b93c9.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/18b93c9.html","path":"post/18b93c9.html","title":"LV10-11-I2C驱动-02-应用层使用I2C驱动"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV10-11-I2C驱动-02-应用层使用I2C驱动 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81linux%E9%85%8D%E7%BD%AE"><span class="nav-text">一、linux配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%AE%BE%E5%A4%87%E6%A0%91"><span class="nav-text">1. 设备树</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%86%85%E6%A0%B8%E9%85%8D%E7%BD%AE"><span class="nav-text">2. 内核配置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E7%BC%96%E8%AF%91%E6%B5%8B%E8%AF%95"><span class="nav-text">3. 编译测试</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81read%E5%92%8Cwrite%E8%AF%BB%E5%86%99I2C"><span class="nav-text">二、read和write读写I2C</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4"><span class="nav-text">1. 使用步骤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-mpu6050-c"><span class="nav-text">2. mpu6050.c</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-read-data-from-mpu6050"><span class="nav-text">2.1 read_data_from_mpu6050()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-write-data-to-mpu6050"><span class="nav-text">2.2 write_data_to_mpu6050()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-init-mpu6050"><span class="nav-text">2.3 init_mpu6050()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-read-accelx"><span class="nav-text">2.4 read_accelx()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-read-accely"><span class="nav-text">2.5 read_accely()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-read-accelz"><span class="nav-text">2.6 read_accelz()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7-read-temp"><span class="nav-text">2.7 read_temp()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-8-read-gyrox"><span class="nav-text">2.8 read_gyrox()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-9-read-gyroy"><span class="nav-text">2.9 read_gyroy()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-10-read-gyroz"><span class="nav-text">2.10 read_gyroz()</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-mpu6050-h"><span class="nav-text">3. mpu6050.h</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-mpu6050-app-c"><span class="nav-text">4. mpu6050_app.c</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E7%BC%96%E8%AF%91%E6%B5%8B%E8%AF%95"><span class="nav-text">5. 编译测试</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81ioctl%E8%AF%BB%E5%86%99I2C"><span class="nav-text">三、ioctl读写I2C</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%9B%B8%E5%85%B3%E7%BB%93%E6%9E%84%E4%BD%93%E5%92%8C%E5%AE%8F"><span class="nav-text">1. 相关结构体和宏</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%B8%89%E4%B8%AA%E5%AE%8F"><span class="nav-text">1.1 三个宏</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-struct-i2c-rdwr-ioctl-data"><span class="nav-text">1.2 struct i2c_rdwr_ioctl_data</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-struct-i2c-msg"><span class="nav-text">1.3 struct i2c_msg</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4"><span class="nav-text">2. 使用步骤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-mpu6050-c"><span class="nav-text">3. mpu6050.c</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-read-data-from-mpu6050"><span class="nav-text">3.1 read_data_from_mpu6050()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-write-data-to-mpu6050"><span class="nav-text">3.2 write_data_to_mpu6050()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-init-mpu6050"><span class="nav-text">3.3 init_mpu6050()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-read-accelx"><span class="nav-text">3.4 read_accelx()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5-read-accely"><span class="nav-text">3.5 read_accely()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6-read-accelz"><span class="nav-text">3.6 read_accelz()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7-read-temp"><span class="nav-text">3.7 read_temp()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-8-read-gyrox"><span class="nav-text">3.8 read_gyrox()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-9-read-gyroy"><span class="nav-text">3.9 read_gyroy()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-10-read-gyroz"><span class="nav-text">3.10 read_gyroz()</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-mpu6050-h"><span class="nav-text">4. mpu6050.h</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-mpu6050-app-c"><span class="nav-text">5. mpu6050_app.c</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E7%BC%96%E8%AF%91%E6%B5%8B%E8%AF%95"><span class="nav-text">6. 编译测试</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/18b93c9.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV10-11-I2C驱动-02-应用层使用I2C驱动 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV10-11-I2C驱动-02-应用层使用I2C驱动
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-08 18:58:38" itemprop="dateCreated datePublished" datetime="2023-07-08T18:58:38+08:00">2023-07-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV10-驱动开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>21 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是Linux中应用层直接使用I2C驱动的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-11-I2C%E9%A9%B1%E5%8A%A8-02-%E5%BA%94%E7%94%A8%E5%B1%82%E4%BD%BF%E7%94%A8I2C%E9%A9%B1%E5%8A%A8/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center" width=150>Linux开发板</td>        <td align="left">华清远见 底板: FS4412_DEV_V5 核心板: FS4412 V2</td>      </tr>    <tr>        <td align="center" width=150>u-boot</td>        <td align="left">2013.01</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向</td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr><td align="center">文件</td><td align="center">下载链接</td></tr>    <tr><td align="left">---</td><td align="left"><a href="https://wwz.lanzouy.com/iH1vO09jxavi" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<p>在linux内核中为我们提供了一个I2C字符设备驱动，我们直接在linux内核中配置使用就可以了。</p>
<h1 id="一、linux配置"><a href="#一、linux配置" class="headerlink" title="一、linux配置"></a><font size=3>一、linux配置</font></h1><h2 id="1-设备树"><a href="#1-设备树" class="headerlink" title="1. 设备树"></a><font size=3>1. 设备树</font></h2><p>exynos4412平台每个i2c通道的信息是通过设备树提供的，因此需要首先在exynos4412-fs4412.dts中增加5通道（MPU6050就挂载在这个通道上）的节点。关于I2C设备树的书写实例，我们可以参考linux内核源码中的这个文档：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Documentation/devicetree/bindings/i2c/i2c-s3c2410.txt</span><br></pre></td></tr></table></figure>

<p>然后我们在linux内核源码的设备树中添加以下内容：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">i2c@<span class="number">138B</span>0000&#123;</span><br><span class="line">    <span class="meta">#address-cells = <span class="string">&lt;1&gt;</span>;</span></span><br><span class="line">    <span class="meta">#size-cells = <span class="string">&lt;0&gt;</span>;</span></span><br><span class="line">    samsung,i2c-sda-delay = &lt;<span class="number">100</span>&gt;;</span><br><span class="line">    samsung,i2c-max-bus-freq = &lt;<span class="number">20000</span>&gt;;</span><br><span class="line">    pinctrl<span class="number">-0</span> = &lt;&amp;i2c5_bus&gt;;</span><br><span class="line">    pinctrl-names = <span class="string">&quot;default&quot;</span>;</span><br><span class="line">    status = <span class="string">&quot;okay&quot;</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="2-内核配置"><a href="#2-内核配置" class="headerlink" title="2. 内核配置"></a><font size=3>2. 内核配置</font></h2><p>另外我们还需要在内核中开启I2C字符设备驱动：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Device Drivers  ---&gt;</span><br><span class="line">	-*- I2C support  ---&gt;</span><br><span class="line">		&lt;*&gt;   I2C device interface</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-11-I2C%E9%A9%B1%E5%8A%A8-02-%E5%BA%94%E7%94%A8%E5%B1%82%E4%BD%BF%E7%94%A8I2C%E9%A9%B1%E5%8A%A8/img/image-20220927162633308.png" alt="image-20220927162633308" style="zoom:50%;" />

<h2 id="3-编译测试"><a href="#3-编译测试" class="headerlink" title="3. 编译测试"></a><font size=3>3. 编译测试</font></h2><p>然后我们就可以重新编译内核和设备树文件，我们在linux内核目录下执行：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">make dtbs    # 编译设备树</span><br><span class="line">make  uImage # 编译内核</span><br></pre></td></tr></table></figure>

<p>编译完成后，我们将内核和设备树文件拷贝到相应的目录中，然后重启开发板，重启之后我们将会在 &#x2F;dev&#x2F;目录下看到i2c开头的设备节点：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@farsight ]# ls /dev/i2c*</span><br><span class="line">i2c-0 i2c-5</span><br></pre></td></tr></table></figure>

<p>当我们读写i2c设备的时候就是通过这个设备节点进行的。</p>
<h1 id="二、read和write读写I2C"><a href="#二、read和write读写I2C" class="headerlink" title="二、read和write读写I2C"></a><font size=3>二、read和write读写I2C</font></h1><p>我们可以直接通过read和write函数读写I2C设备，此处以MPU6050为例。</p>
<h2 id="1-使用步骤"><a href="#1-使用步骤" class="headerlink" title="1. 使用步骤"></a><font size=3>1. 使用步骤</font></h2><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-11-I2C%E9%A9%B1%E5%8A%A8-02-%E5%BA%94%E7%94%A8%E5%B1%82%E4%BD%BF%E7%94%A8I2C%E9%A9%B1%E5%8A%A8/img/01.png" alt="01" style="zoom:50%;" />

<h2 id="2-mpu6050-c"><a href="#2-mpu6050-c" class="headerlink" title="2. mpu6050.c"></a><font size=3>2. mpu6050.c</font></h2><h3 id="2-1-read-data-from-mpu6050"><a href="#2-1-read-data-from-mpu6050" class="headerlink" title="2.1 read_data_from_mpu6050()"></a><font size=3>2.1 read_data_from_mpu6050()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_data_from_mpu6050</span></span><br><span class="line"><span class="comment"> * @brief      : 从MPU6050指定寄存器读一个字节数据</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @param reg  : unsigned char 类型，寄存器地址</span></span><br><span class="line"><span class="comment"> * @param pdata: unsigned char * 类型，读取到的数据存放内存空间的首地址</span></span><br><span class="line"><span class="comment"> * @return     : int类型，成功返回0,失败返回-1</span></span><br><span class="line"><span class="comment"> * @Description: 内核中会自己完成 I2C 器件寻址，以及读写的判断，所以这里我们就直接发送寄存器地址即可</span></span><br><span class="line"><span class="comment"> *               初始化MPU6050的时候将从机地址设置到 fd 关联的 I2C_dev上</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">read_data_from_mpu6050</span><span class="params">(<span class="type">int</span> fd, <span class="type">unsigned</span> <span class="type">char</span> reg, <span class="type">unsigned</span> <span class="type">char</span> *pdata)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 0.相关变量定义 */</span></span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> buf[<span class="number">1</span>] = &#123;reg&#125;;</span><br><span class="line">    <span class="comment">/* 1.向从机发送要读的寄存器 */</span></span><br><span class="line">    ret = write(fd, buf, <span class="number">1</span>); <span class="comment">/*  */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;write reg failed,in read_data_from_mpu6050!\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 3.读取数据 */</span></span><br><span class="line">    buf[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">    ret = read(fd, buf, <span class="number">1</span>); <span class="comment">/* 读一个字节的数据 */</span></span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read data failed,in read_data_from_mpu6050!\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    *pdata = buf[<span class="number">0</span>];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-2-write-data-to-mpu6050"><a href="#2-2-write-data-to-mpu6050" class="headerlink" title="2.2 write_data_to_mpu6050()"></a><font size=3>2.2 write_data_to_mpu6050()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : write_data_to_mpu6050</span></span><br><span class="line"><span class="comment"> * @brief      : 向MPU6050指定寄存器写一个字节数据</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @param reg  : unsigned char 类型，寄存器地址</span></span><br><span class="line"><span class="comment"> * @param data : unsigned char 类型，要写入的数据</span></span><br><span class="line"><span class="comment"> * @return     : int类型，成功返回0,失败返回-1</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">write_data_to_mpu6050</span><span class="params">(<span class="type">int</span> fd, <span class="type">unsigned</span> <span class="type">char</span> reg, <span class="type">unsigned</span> <span class="type">char</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 0.相关变量定义 */</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> buf[<span class="number">2</span>] = &#123;reg, data&#125;;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">/* 1.向MPU6050发送要写入的寄存器地址和要写入的数据 */</span></span><br><span class="line">    ret = write(fd, buf, <span class="number">2</span>);</span><br><span class="line">    <span class="keyword">if</span> (ret != <span class="number">2</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;write data failed,in write_data_to_mpu6050\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-3-init-mpu6050"><a href="#2-3-init-mpu6050" class="headerlink" title="2.3 init_mpu6050()"></a><font size=3>2.3 init_mpu6050()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : init_mpu6050</span></span><br><span class="line"><span class="comment"> * @brief      : 初始化MPU6050</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，成功返回0,失败返回-1</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">init_mpu6050</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 0.相关变量定义 */</span></span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">/* 1.设置I2C使用7位地址 */</span></span><br><span class="line">    ret = ioctl(fd, I2C_TENBIT, <span class="number">0</span>); <span class="comment">/* 0表示不使用10位地址，使用7位地址 */</span></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;ioctl I2C_TENBIT failed,in init_mpu6050\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 2.将从设备地址关联到I2C */</span></span><br><span class="line">    ret = ioctl(fd, I2C_SLAVE, <span class="number">0x68</span>);</span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;ioctl I2C_TENBIT failed,in init_mpu6050\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 3.设置MPU6050 */</span></span><br><span class="line">    ret = write_data_to_mpu6050(fd, PWR_MGMT_1, <span class="number">0x00</span>);    <span class="comment">/* 电源管理，典型值：0x00(正常启用) */</span></span><br><span class="line">    ret += write_data_to_mpu6050(fd, SMPLRT_DIV, <span class="number">0x07</span>);   <span class="comment">/* 陀螺仪采样率 */</span></span><br><span class="line">    ret += write_data_to_mpu6050(fd, ACCEL_CONFIG, <span class="number">0x19</span>); <span class="comment">/* 加速计自检、测量范围及高通滤波频率，加速计自检、测量范围，典型值：0x19(不自检，+/-G) */</span></span><br><span class="line">    ret += write_data_to_mpu6050(fd, GYRO_CONFIG, <span class="number">0xF8</span>);  <span class="comment">/* 陀螺仪自检及测量范围，典型值：0xF8(不自检，+/-2000deg/s) */</span></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;write init data to mpu6050 failed,in init_mpu6050\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-4-read-accelx"><a href="#2-4-read-accelx" class="headerlink" title="2.4 read_accelx()"></a><font size=3>2.4 read_accelx()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_accelx</span></span><br><span class="line"><span class="comment"> * @brief      : 读取x方向加速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accelx</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, ACCEL_XOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, ACCEL_XOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read accel x value failed,in read_accelx\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-5-read-accely"><a href="#2-5-read-accely" class="headerlink" title="2.5 read_accely()"></a><font size=3>2.5 read_accely()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_accely</span></span><br><span class="line"><span class="comment"> * @brief      : 读取y方向加速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accely</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, ACCEL_YOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, ACCEL_YOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read accel y value failed,in read_accely\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-6-read-accelz"><a href="#2-6-read-accelz" class="headerlink" title="2.6 read_accelz()"></a><font size=3>2.6 read_accelz()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_accelz</span></span><br><span class="line"><span class="comment"> * @brief      : 读取z方向加速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accelz</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, ACCEL_ZOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, ACCEL_ZOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read accel z value failed,in read_accelz\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-7-read-temp"><a href="#2-7-read-temp" class="headerlink" title="2.7 read_temp()"></a><font size=3>2.7 read_temp()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_temp</span></span><br><span class="line"><span class="comment"> * @brief      : 读取温度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_temp</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, TEMP_OUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, TEMP_OUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read temp value failed,in read_temp\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-8-read-gyrox"><a href="#2-8-read-gyrox" class="headerlink" title="2.8 read_gyrox()"></a><font size=3>2.8 read_gyrox()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_gyrox</span></span><br><span class="line"><span class="comment"> * @brief      : 读取x方向角速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyrox</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, GYRO_XOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, GYRO_XOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read gyro x value failed,in read_gyrox\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-9-read-gyroy"><a href="#2-9-read-gyroy" class="headerlink" title="2.9 read_gyroy()"></a><font size=3>2.9 read_gyroy()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_gyroy</span></span><br><span class="line"><span class="comment"> * @brief      : 读取y方向角速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyroy</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, GYRO_YOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, GYRO_YOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read gyro y value failed,in read_gyroy\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="2-10-read-gyroz"><a href="#2-10-read-gyroz" class="headerlink" title="2.10 read_gyroz()"></a><font size=3>2.10 read_gyroz()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_gyroz</span></span><br><span class="line"><span class="comment"> * @brief      : 读取z方向角速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyroz</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, GYRO_ZOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, GYRO_ZOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read gyro z value failed,in read_gyroz\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="3-mpu6050-h"><a href="#3-mpu6050-h" class="headerlink" title="3. mpu6050.h"></a><font size=3>3. mpu6050.h</font></h2><details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifndef</span> __MPU6050_H__</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __MPU6050_H__</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/ioctl.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 宏定义 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>	SMPLRT_DIV	  0x19	<span class="comment">/* 陀螺仪采样率，典型值：0x07(125Hz) */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>	CONFIG		  0x1A	<span class="comment">/* 低通滤波频率，典型值：0x06(5Hz) */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>	GYRO_CONFIG	  0x1B	<span class="comment">/* 陀螺仪自检及测量范围，典型值：0xF8(不自检，+/-2000deg/s) */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>	ACCEL_CONFIG  0x1C	<span class="comment">/* 加速计自检、测量范围及高通滤波频率，加速计自检、测量范围，典型值：0x19(不自检，+/-G) */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_XOUT_H  0x3B</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_XOUT_L  0x3C</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_YOUT_H  0x3D</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_YOUT_L  0x3E</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_ZOUT_H  0x3F</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_ZOUT_L  0x40</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> TEMP_OUT_H    0x41</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> TEMP_OUT_L    0x42</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_XOUT_H   0x43</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_XOUT_L   0x44</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_YOUT_H   0x45</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_YOUT_L   0x46</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_ZOUT_H   0x47</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_ZOUT_L   0x48</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PWR_MGMT_1    0x6B    <span class="comment">/* 电源管理，典型值：0x00(正常启用) */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 原来定义在内核源码的 include/uapi/linux/i2c/i2c-dev.h */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_SLAVE     0x0703  <span class="comment">/* Use this slave address */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_TENBIT    0x0704  <span class="comment">/* 0 for 7 bit addrs, != 0 for 10 bit */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* printk和printf 打印输出的颜色定义 */</span></span><br><span class="line"><span class="comment">/* 前景色(字体颜色) */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CLS           <span class="string">&quot;\033[0m&quot;</span>    <span class="comment">/* 清除所有颜色 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLACK         <span class="string">&quot;\033[1;30m&quot;</span> <span class="comment">/* 黑色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> RED           <span class="string">&quot;\033[1;31m&quot;</span> <span class="comment">/* 红色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GREEN         <span class="string">&quot;\033[1;32m&quot;</span> <span class="comment">/* 绿色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> YELLOW        <span class="string">&quot;\033[1;33m&quot;</span> <span class="comment">/* 黄色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BLUE          <span class="string">&quot;\033[1;34m&quot;</span> <span class="comment">/* 蓝色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PURPLE        <span class="string">&quot;\033[1;35m&quot;</span> <span class="comment">/* 紫色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> CYAN          <span class="string">&quot;\033[1;36m&quot;</span> <span class="comment">/* 青色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> WHITE         <span class="string">&quot;\033[1;37m&quot;</span> <span class="comment">/* 白色加粗字体 */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BOLD          <span class="string">&quot;\033[1m&quot;</span>    <span class="comment">/* 加粗字体    */</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">init_mpu6050</span><span class="params">(<span class="type">int</span> fd)</span>; <span class="comment">/* 初始化 MPU6050 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accelx</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accely</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accelz</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_temp</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyrox</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyroy</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyroz</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="4-mpu6050-app-c"><a href="#4-mpu6050-app-c" class="headerlink" title="4. mpu6050_app.c"></a><font size=3>4. mpu6050_app.c</font></h2><details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span>     <span class="comment">/* printf */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span> <span class="comment">/* open */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/stat.h&gt;</span>  <span class="comment">/* open */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fcntl.h&gt;</span>     <span class="comment">/* open */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span>    <span class="comment">/* close sleep */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/ioctl.h&gt;</span> <span class="comment">/* ioctl */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;mpu6050.h&quot;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 0.相关变量定义 */</span></span><br><span class="line">    <span class="type">int</span> fd = <span class="number">-1</span>; <span class="comment">/* 文件描述符 */</span></span><br><span class="line">    <span class="comment">/* 1.判断参数个数是否合法 */</span></span><br><span class="line">    <span class="comment">/* 1.1判断参数数量 */</span></span><br><span class="line">    <span class="keyword">if</span> (argc &lt; <span class="number">2</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\nusage:\n&quot;</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%s /dev/dev_name\n&quot;</span>, argv[<span class="number">0</span>]);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 2.打开字符设备 */</span></span><br><span class="line">    <span class="keyword">if</span> ((fd = open(argv[<span class="number">1</span>], O_RDWR)) &lt; <span class="number">0</span>) <span class="comment">/* 默认是阻塞的 */</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;open %s failed!\n&quot;</span>, argv[<span class="number">1</span>]);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 3.初始化 MPU6050 */</span></span><br><span class="line">    init_mpu6050(fd);</span><br><span class="line">    <span class="comment">/* 4.循环读取并打印数据 */</span></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        sleep(<span class="number">2</span>);</span><br><span class="line">        <span class="comment">/* read and print data from MPU6050 */</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Accel-X:0x%x\n&quot;</span>, read_accelx(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Accel-Y:0x%x\n&quot;</span>, read_accely(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Accel-Z:0x%x\n&quot;</span>, read_accelz(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Temp:0x%x\n&quot;</span>, read_temp(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;GYRO-X:0x%x\n&quot;</span>, read_gyrox(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;GYRO-Y:0x%x\n&quot;</span>, read_gyroy(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;GYRO-z:0x%x\n&quot;</span>, read_gyroz(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 4.关闭字符设备 */</span></span><br><span class="line">    close(fd);</span><br><span class="line">    fd = <span class="number">-1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="5-编译测试"><a href="#5-编译测试" class="headerlink" title="5. 编译测试"></a><font size=3>5. 编译测试</font></h2><p>编译的时候注意使用交叉编译工具链，测试的时候，我们使用的设备节点是在系统启动的时候就创建好了的，使用的设备节点是：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/dev/i2c-5</span><br></pre></td></tr></table></figure>

<h1 id="三、ioctl读写I2C"><a href="#三、ioctl读写I2C" class="headerlink" title="三、ioctl读写I2C"></a><font size=3>三、ioctl读写I2C</font></h1><p>我们可以通过ioctl一个函数读写I2C设备，此处以MPU6050为例。</p>
<h2 id="1-相关结构体和宏"><a href="#1-相关结构体和宏" class="headerlink" title="1. 相关结构体和宏"></a><font size=3>1. 相关结构体和宏</font></h2><p>只使用ioctl来读写i2c设备的话，我们需要用到3个宏和两个结构体，不过为了方便，这几个会在mpu6050.h中重新实现。</p>
<h3 id="1-1-三个宏"><a href="#1-1-三个宏" class="headerlink" title="1.1 三个宏"></a><font size=3>1.1 三个宏</font></h3><p>这里需要用到三个宏定义，它们定义在linux内核源码的这个目录下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">include/uapi/linux/i2c-dev.h</span><br></pre></td></tr></table></figure>

<p>我们可以看到这三个宏定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_SLAVE     0x0703  <span class="comment">/* Use this slave address */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_TENBIT    0x0704  <span class="comment">/* 0 for 7 bit addrs, != 0 for 10 bit */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_RDWR      0x0707  <span class="comment">/* Combined R/W transfer (one STOP only) */</span></span></span><br></pre></td></tr></table></figure>

<p>后边为了方便使用，将这三个宏定义定义到 mpu6050.h 文件中去。</p>
<h3 id="1-2-struct-i2c-rdwr-ioctl-data"><a href="#1-2-struct-i2c-rdwr-ioctl-data" class="headerlink" title="1.2 struct i2c_rdwr_ioctl_data"></a><font size=3>1.2 struct i2c_rdwr_ioctl_data</font></h3><p>这个结构体定义在linux内核源码的这个目录下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">include/uapi/linux/i2c-dev.h</span><br></pre></td></tr></table></figure>

<p>我们打开这个文件，会看到结构体定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* This is the structure as used in the I2C_RDWR ioctl call */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">i2c_rdwr_ioctl_data</span> &#123;</span></span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">i2c_msg</span> __<span class="title">user</span> *<span class="title">msgs</span>;</span>    <span class="comment">/* pointers to i2c_msgs */</span></span><br><span class="line">        __u32 nmsgs;                    <span class="comment">/* number of i2c_msgs */</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>【成员说明】</p>
<ul>
<li>msgs ： struct i2c_msg类型指针变量，指向要发送给i2c设备的消息的首地址。</li>
<li>nmsgs ： __u32 类型，表示有几个消息需要发送。</li>
</ul>
<h3 id="1-3-struct-i2c-msg"><a href="#1-3-struct-i2c-msg" class="headerlink" title="1.3 struct i2c_msg"></a><font size=3>1.3 struct i2c_msg</font></h3><p>这个结构体定义在linux内核中的这个目录下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">include/uapi/linux/i2c.h</span><br></pre></td></tr></table></figure>

<p>我们打开这个文件，该结构体定义如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">i2c_msg</span> &#123;</span></span><br><span class="line">        __u16 addr;     <span class="comment">/* slave address                        */</span></span><br><span class="line">        __u16 flags;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_TEN               0x0010  <span class="comment">/* this is a ten bit chip address */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_RD                0x0001  <span class="comment">/* read data, from slave to master */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_STOP              0x8000  <span class="comment">/* if I2C_FUNC_PROTOCOL_MANGLING */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_NOSTART           0x4000  <span class="comment">/* if I2C_FUNC_NOSTART */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_REV_DIR_ADDR      0x2000  <span class="comment">/* if I2C_FUNC_PROTOCOL_MANGLING */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_IGNORE_NAK        0x1000  <span class="comment">/* if I2C_FUNC_PROTOCOL_MANGLING */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_NO_RD_ACK         0x0800  <span class="comment">/* if I2C_FUNC_PROTOCOL_MANGLING */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_RECV_LEN          0x0400  <span class="comment">/* length will be first received byte */</span></span></span><br><span class="line">        __u16 len;              <span class="comment">/* msg length                           */</span></span><br><span class="line">        __u8 *buf;              <span class="comment">/* pointer to msg data                  */</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p><strong>【成员介绍】</strong></p>
<ul>
<li>addr ：从机的地址</li>
<li>flags ： 一个标志，它表示从机设备地址位数，值为0的话表示使用7位从设备地址。</li>
<li>buf ：我们要发送或者接收数据的内存空间首地址</li>
<li>len ：表示buf中有几个数据需要发送或者接收，一般来说向i2c设备写数据的话，这个值一般会是2，一个表示寄存器地址，一个表示要写入的值，从i2c设备读数据的话，一般会是1。</li>
</ul>
<h2 id="2-使用步骤"><a href="#2-使用步骤" class="headerlink" title="2. 使用步骤"></a><font size=3>2. 使用步骤</font></h2><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/LV10-11-I2C%E9%A9%B1%E5%8A%A8-02-%E5%BA%94%E7%94%A8%E5%B1%82%E4%BD%BF%E7%94%A8I2C%E9%A9%B1%E5%8A%A8/img/02.png" alt="02" style="zoom:50%;" />

<h2 id="3-mpu6050-c"><a href="#3-mpu6050-c" class="headerlink" title="3. mpu6050.c"></a><font size=3>3. mpu6050.c</font></h2><h3 id="3-1-read-data-from-mpu6050"><a href="#3-1-read-data-from-mpu6050" class="headerlink" title="3.1 read_data_from_mpu6050()"></a><font size=3>3.1 read_data_from_mpu6050()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_data_from_mpu6050</span></span><br><span class="line"><span class="comment"> * @brief      : 从MPU6050指定寄存器读一个字节数据</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @param slave: unsigned char 类型，从机地址</span></span><br><span class="line"><span class="comment"> * @param reg  : unsigned char 类型，寄存器地址</span></span><br><span class="line"><span class="comment"> * @param pdata: unsigned char * 类型，读取到的数据存放内存空间的首地址</span></span><br><span class="line"><span class="comment"> * @return     : int类型，成功返回0,失败返回-1</span></span><br><span class="line"><span class="comment"> * @Description: </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">read_data_from_mpu6050</span><span class="params">(<span class="type">int</span> fd, <span class="type">unsigned</span> <span class="type">char</span> slave, <span class="type">unsigned</span> <span class="type">char</span> reg, <span class="type">unsigned</span> <span class="type">char</span> *pdata)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 0.相关变量定义 */</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">i2c_rdwr_ioctl_data</span> <span class="title">work</span> =</span> &#123;<span class="literal">NULL</span>&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">i2c_msg</span> <span class="title">msgs</span>[2] =</span> &#123;&#123;<span class="number">0</span>&#125;&#125;;<span class="comment">/* 数组元素有两个，一个用于发送数据，一个用于接收数据 */</span></span><br><span class="line"></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> buf1[<span class="number">1</span>] = &#123;reg&#125;; <span class="comment">/* 存储寄存器地址 */</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> buf2[<span class="number">1</span>] = &#123;<span class="number">0</span>&#125;;   <span class="comment">/* 存储接收的数据 */</span></span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">/* 1.变量赋值 */</span></span><br><span class="line">    work.msgs = msgs;</span><br><span class="line">    work.nmsgs = <span class="number">2</span>;</span><br><span class="line">    <span class="comment">/* msgs[0]负责寻址，完成发送从设备地址和要读取的寄存器的地址 */</span></span><br><span class="line">    msgs[<span class="number">0</span>].addr = slave;</span><br><span class="line">    msgs[<span class="number">0</span>].flags = <span class="number">0</span>; <span class="comment">/* 表示使用7位从设备地址 */</span></span><br><span class="line">    msgs[<span class="number">0</span>].buf = buf1;</span><br><span class="line">    msgs[<span class="number">0</span>].len = <span class="number">1</span>;</span><br><span class="line">    <span class="comment">/* msgs[0]负责接收读到的数据，完成 slave_addr + 1，并读取相应寄存器数据 */</span></span><br><span class="line">    msgs[<span class="number">1</span>].addr = slave;</span><br><span class="line">    msgs[<span class="number">1</span>].flags = I2C_M_RD;</span><br><span class="line">    msgs[<span class="number">1</span>].buf = buf2;</span><br><span class="line">    msgs[<span class="number">1</span>].len = <span class="number">1</span>;</span><br><span class="line">    <span class="comment">/* 3.读取数据 */</span></span><br><span class="line">    ret = ioctl(fd, I2C_RDWR, &amp;work);</span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;ioctl I2C_RDWR failed,in read_data_from_mpu6050!\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        *pdata = buf2[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-2-write-data-to-mpu6050"><a href="#3-2-write-data-to-mpu6050" class="headerlink" title="3.2 write_data_to_mpu6050()"></a><font size=3>3.2 write_data_to_mpu6050()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : write_data_to_mpu6050</span></span><br><span class="line"><span class="comment"> * @brief      : 向MPU6050指定寄存器写一个字节数据</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @param slave: unsigned char 类型，从机地址</span></span><br><span class="line"><span class="comment"> * @param reg  : unsigned char 类型，寄存器地址</span></span><br><span class="line"><span class="comment"> * @param data : unsigned char 类型，要写入的数据</span></span><br><span class="line"><span class="comment"> * @return     : int类型，成功返回0,失败返回-1</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">static</span> <span class="type">int</span> <span class="title function_">write_data_to_mpu6050</span><span class="params">(<span class="type">int</span> fd, <span class="type">unsigned</span> <span class="type">char</span> slave, <span class="type">unsigned</span> <span class="type">char</span> reg, <span class="type">unsigned</span> <span class="type">char</span> data)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 0.相关变量定义 */</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">i2c_rdwr_ioctl_data</span> <span class="title">work</span> =</span> &#123;<span class="literal">NULL</span>&#125;;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">i2c_msg</span> <span class="title">msg</span> =</span> &#123;<span class="number">0</span>&#125;;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> buf[<span class="number">2</span>] = &#123;reg, data&#125;;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">/* 1.变量赋值 */</span></span><br><span class="line">    work.msgs = &amp;msg;</span><br><span class="line">    work.nmsgs = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    msg.addr = slave;</span><br><span class="line">    msg.flags = <span class="number">0</span>;</span><br><span class="line">    msg.buf = buf;</span><br><span class="line">    msg.len = <span class="number">2</span>;</span><br><span class="line">    <span class="comment">/* 2.发送数据 */</span></span><br><span class="line">    ret = ioctl(fd, I2C_RDWR, &amp;work);</span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;ioctl I2C_RDWR failed,in write_data_to_mpu6050!\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-3-init-mpu6050"><a href="#3-3-init-mpu6050" class="headerlink" title="3.3 init_mpu6050()"></a><font size=3>3.3 init_mpu6050()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : init_mpu6050</span></span><br><span class="line"><span class="comment"> * @brief      : 初始化MPU6050</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，成功返回0,失败返回-1</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">init_mpu6050</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 0.相关变量定义 */</span></span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">/* 1.设置I2C使用7位地址 */</span></span><br><span class="line">    ret = ioctl(fd, I2C_TENBIT, <span class="number">0</span>); <span class="comment">/* 0表示不使用10位地址，使用7位地址 */</span></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;ioctl I2C_TENBIT failed,in init_mpu6050\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 2.将从设备地址关联到I2C */</span></span><br><span class="line">    ret = ioctl(fd, I2C_SLAVE, SLAVE_ADDRESS);</span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;ioctl I2C_TENBIT failed,in init_mpu6050\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 3.设置MPU6050 */</span></span><br><span class="line">    ret = write_data_to_mpu6050(fd, SLAVE_ADDRESS, PWR_MGMT_1, <span class="number">0x00</span>);    <span class="comment">/* 电源管理，典型值：0x00(正常启用) */</span></span><br><span class="line">    ret += write_data_to_mpu6050(fd, SLAVE_ADDRESS, SMPLRT_DIV, <span class="number">0x07</span>);   <span class="comment">/* 陀螺仪采样率 */</span></span><br><span class="line">    ret += write_data_to_mpu6050(fd, SLAVE_ADDRESS, ACCEL_CONFIG, <span class="number">0x19</span>); <span class="comment">/* 加速计自检、测量范围及高通滤波频率，加速计自检、测量范围，典型值：0x19(不自检，+/-G) */</span></span><br><span class="line">    ret += write_data_to_mpu6050(fd, SLAVE_ADDRESS, GYRO_CONFIG, <span class="number">0xF8</span>);  <span class="comment">/* 陀螺仪自检及测量范围，典型值：0xF8(不自检，+/-2000deg/s) */</span></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;write init data to mpu6050 failed,in init_mpu6050\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-4-read-accelx"><a href="#3-4-read-accelx" class="headerlink" title="3.4 read_accelx()"></a><font size=3>3.4 read_accelx()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_accelx</span></span><br><span class="line"><span class="comment"> * @brief      : 读取x方向加速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accelx</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, ACCEL_XOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, ACCEL_XOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read accel x value failed,in read_accelx\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-5-read-accely"><a href="#3-5-read-accely" class="headerlink" title="3.5 read_accely()"></a><font size=3>3.5 read_accely()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_accely</span></span><br><span class="line"><span class="comment"> * @brief      : 读取y方向加速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accely</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, ACCEL_YOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, ACCEL_YOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read accel y value failed,in read_accely\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-6-read-accelz"><a href="#3-6-read-accelz" class="headerlink" title="3.6 read_accelz()"></a><font size=3>3.6 read_accelz()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_accelz</span></span><br><span class="line"><span class="comment"> * @brief      : 读取z方向加速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accelz</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, ACCEL_ZOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, ACCEL_ZOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read accel z value failed,in read_accelz\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-7-read-temp"><a href="#3-7-read-temp" class="headerlink" title="3.7 read_temp()"></a><font size=3>3.7 read_temp()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_temp</span></span><br><span class="line"><span class="comment"> * @brief      : 读取温度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_temp</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, TEMP_OUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, TEMP_OUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read temp value failed,in read_temp\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-8-read-gyrox"><a href="#3-8-read-gyrox" class="headerlink" title="3.8 read_gyrox()"></a><font size=3>3.8 read_gyrox()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_gyrox</span></span><br><span class="line"><span class="comment"> * @brief      : 读取x方向角速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyrox</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, GYRO_XOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, GYRO_XOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read gyro x value failed,in read_gyrox\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-9-read-gyroy"><a href="#3-9-read-gyroy" class="headerlink" title="3.9 read_gyroy()"></a><font size=3>3.9 read_gyroy()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_gyroy</span></span><br><span class="line"><span class="comment"> * @brief      : 读取y方向角速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyroy</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, GYRO_YOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, GYRO_YOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read gyro y value failed,in read_gyroy\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h3 id="3-10-read-gyroz"><a href="#3-10-read-gyroz" class="headerlink" title="3.10 read_gyroz()"></a><font size=3>3.10 read_gyroz()</font></h3><details class="folding-tag" blue><summary> 点击查看函数详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @Function   : read_gyroz</span></span><br><span class="line"><span class="comment"> * @brief      : 读取z方向角速度值</span></span><br><span class="line"><span class="comment"> * @param fd   : int 类型，文件描述符，代表设备</span></span><br><span class="line"><span class="comment"> * @return     : int类型，读取到的数据</span></span><br><span class="line"><span class="comment"> * @Description:</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyroz</span><span class="params">(<span class="type">int</span> fd)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> val = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> d = <span class="number">0</span>;</span><br><span class="line">    <span class="type">int</span> ret = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, GYRO_ZOUT_L, &amp;d);</span><br><span class="line">    val = d;</span><br><span class="line"></span><br><span class="line">    ret = read_data_from_mpu6050(fd, SLAVE_ADDRESS, GYRO_ZOUT_H, &amp;d);</span><br><span class="line">    val |= d &lt;&lt; <span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ret &lt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;read gyro z value failed,in read_gyroz\n&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> val;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="4-mpu6050-h"><a href="#4-mpu6050-h" class="headerlink" title="4. mpu6050.h"></a><font size=3>4. mpu6050.h</font></h2><details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">ifndef</span> __MPU6050_H__</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> __MPU6050_H__</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 头文件 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/stat.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fcntl.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/ioctl.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">/* 宏定义 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>	SMPLRT_DIV	  0x19	<span class="comment">/* 陀螺仪采样率，典型值：0x07(125Hz) */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>	CONFIG		  0x1A	<span class="comment">/* 低通滤波频率，典型值：0x06(5Hz) */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>	GYRO_CONFIG	  0x1B	<span class="comment">/* 陀螺仪自检及测量范围，典型值：0xF8(不自检，+/-2000deg/s) */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span>	ACCEL_CONFIG  0x1C	<span class="comment">/* 加速计自检、测量范围及高通滤波频率，加速计自检、测量范围，典型值：0x19(不自检，+/-G) */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_XOUT_H  0x3B</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_XOUT_L  0x3C</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_YOUT_H  0x3D</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_YOUT_L  0x3E</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_ZOUT_H  0x3F</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ACCEL_ZOUT_L  0x40</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> TEMP_OUT_H    0x41</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> TEMP_OUT_L    0x42</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_XOUT_H   0x43</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_XOUT_L   0x44</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_YOUT_H   0x45</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_YOUT_L   0x46</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_ZOUT_H   0x47</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GYRO_ZOUT_L   0x48</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> SLAVE_ADDRESS 0x68</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PWR_MGMT_1    0x6B    <span class="comment">/* 电源管理，典型值：0x00(正常启用) */</span></span></span><br><span class="line"><span class="comment">/* 原来定义在内核源码的 include/uapi/linux/i2c-dev.h */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_SLAVE     0x0703  <span class="comment">/* Use this slave address */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_TENBIT    0x0704 <span class="comment">/* 0 for 7 bit addrs, != 0 for 10 bit */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_RDWR      0x0707   <span class="comment">/* Combined R/W transfer (one STOP only) */</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">i2c_msg</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> addr;   <span class="comment">/* slave address */</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> flags;  <span class="comment">/* 一个标志 */</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_TEN  0x0010  <span class="comment">/* this is a ten bit chip address */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> I2C_M_RD   0x0001  <span class="comment">/* read data, from slave to master */</span></span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">short</span> len;    <span class="comment">/* msg length （buf的大小）*/</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> *buf;    <span class="comment">/* pointer to msg data（要发送的数据和要接收的数据都存储在这里）*/</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** 原来定义在内核源码的 include/uapi/linux/i2c-dev.h</span></span><br><span class="line"><span class="comment"> *  This is the structure as used in the I2C_RDWR ioctl call </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">i2c_rdwr_ioctl_data</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">i2c_msg</span> *<span class="title">msgs</span>;</span> <span class="comment">/* pointers to i2c_msgs */</span></span><br><span class="line">    <span class="type">unsigned</span> <span class="type">int</span> nmsgs;   <span class="comment">/* number of i2c_msgs */</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">init_mpu6050</span><span class="params">(<span class="type">int</span> fd)</span>; <span class="comment">/* 初始化 MPU6050 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accelx</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accely</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_accelz</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_temp</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyrox</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyroy</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">read_gyroz</span><span class="params">(<span class="type">int</span> fd)</span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="5-mpu6050-app-c"><a href="#5-mpu6050-app-c" class="headerlink" title="5. mpu6050_app.c"></a><font size=3>5. mpu6050_app.c</font></h2><details class="folding-tag" blue><summary> 点击查看详情 </summary>
              <div class='content'>
              <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span>     <span class="comment">/* printf */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/types.h&gt;</span> <span class="comment">/* open */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/stat.h&gt;</span>  <span class="comment">/* open */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fcntl.h&gt;</span>     <span class="comment">/* open */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span>    <span class="comment">/* close sleep */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;sys/ioctl.h&gt;</span> <span class="comment">/* ioctl */</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;mpu6050.h&quot;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* 0.相关变量定义 */</span></span><br><span class="line">    <span class="type">int</span> fd = <span class="number">-1</span>; <span class="comment">/* 文件描述符 */</span></span><br><span class="line">    <span class="comment">/* 1.判断参数个数是否合法 */</span></span><br><span class="line">    <span class="comment">/* 1.1判断参数数量 */</span></span><br><span class="line">    <span class="keyword">if</span> (argc &lt; <span class="number">2</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\nusage:\n&quot;</span>);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%s /dev/dev_name\n&quot;</span>, argv[<span class="number">0</span>]);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 2.打开字符设备 */</span></span><br><span class="line">    <span class="keyword">if</span> ((fd = open(argv[<span class="number">1</span>], O_RDWR)) &lt; <span class="number">0</span>) <span class="comment">/* 默认是阻塞的 */</span></span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;open %s failed!\n&quot;</span>, argv[<span class="number">1</span>]);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 3.初始化 MPU6050 */</span></span><br><span class="line">    init_mpu6050(fd);</span><br><span class="line">    <span class="comment">/* 4.循环读取并打印数据 */</span></span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        sleep(<span class="number">2</span>);</span><br><span class="line">        <span class="comment">/* read and print data from MPU6050 */</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Accel-X:0x%x\n&quot;</span>, read_accelx(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Accel-Y:0x%x\n&quot;</span>, read_accely(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Accel-Z:0x%x\n&quot;</span>, read_accelz(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;Temp:0x%x\n&quot;</span>, read_temp(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;GYRO-X:0x%x\n&quot;</span>, read_gyrox(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;GYRO-Y:0x%x\n&quot;</span>, read_gyroy(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;GYRO-z:0x%x\n&quot;</span>, read_gyroz(fd));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* 4.关闭字符设备 */</span></span><br><span class="line">    close(fd);</span><br><span class="line">    fd = <span class="number">-1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
              </div>
            </details>

<h2 id="6-编译测试"><a href="#6-编译测试" class="headerlink" title="6. 编译测试"></a><font size=3>6. 编译测试</font></h2><p>编译的时候注意使用交叉编译工具链，测试的时候，我们使用的设备节点是在系统启动的时候就创建好了的，使用的设备节点是：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/dev/i2c-5</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/18b93c9.html">LV10-11-I2C驱动-02-应用层使用I2C驱动</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月08日 - 18:58</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/18b93c9.html" title="LV10-11-I2C驱动-02-应用层使用I2C驱动">https://sumumm.github.io/post/18b93c9.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV10-%E9%A9%B1%E5%8A%A8%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV10-驱动开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/56147393.html" rel="prev" title="LV10-11-I2C驱动-03-I2C二级外设驱动">
                  <i class="fa fa-angle-left"></i> LV10-11-I2C驱动-03-I2C二级外设驱动
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/b1c68daf.html" rel="next" title="LV10-11-I2C驱动-01-Linux中的I2C驱动">
                  LV10-11-I2C驱动-01-Linux中的I2C驱动 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
